<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('admin'.EXT);
/**
 * @author  Nguyen Xuan Hung
 * @email   hungnguyen@vietnambiz.com
 * @date    09.02.2011
 */

class User extends Admin {
    
    function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
    }
    
    function index($page = 1)
    {
        $limit  = $this->config->item('user', 'limit');
        $offset = ($page - 1) * $limit;
        
        $this->data['page'] = $page;
        $this->data['limit'] = $limit;
        
        if($status = $this->input->get('status')) {
        	$this->data['user_list'] = $this->user_model->limit($limit, $offset)->get_many_by('status', $status);
        }
        else {
        	$this->data['user_list'] = $this->user_model->limit($limit, $offset)->get_all();
        }
 
    	if($this->input->is_ajax_request()) {
			$this->load->view('user/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'user/index';
			$this->load->view('layout/default', $this->data);
		}
    }
    
    function change_pass()
    {
    	if($_SERVER['REQUEST_METHOD'] == 'POST') 
    	{
    		$this->form_validation->set_rules('old_password','Mật khẩu cũ','required|trim|xss_clean|callback__check_login');	
    		$this->form_validation->set_rules('new_password','Mật khẩu mới','required|trim|xss_clean|min_length[6]|max_length[12]');
    		$this->form_validation->set_rules('c_password','Mật khẩu xác nhận','required|trim|xss_clean|matches[new_password]');
    		
    		if($this->form_validation->run()===TRUE)
    		{
    			$this->user_model->update($_SESSION['_admin']['user_id'],array('password' => md5($this->input->post('new_password'))));
    			redirect(base_url().'acp_admin/login/');
    			#echo "adasd";
    		}
    		else
    		{
    			$this->message->error	=	$this->form_validation->get_error_array();
    			#echo "false";
    		}
    	}
    	
    	$this->data['tpl_file']		=	'admin/user/change_pass';
    	$this->load->view('layout/default', $this->data);
    }
    
    function _check_login($pass)
    {
    	if(!$this->user_model->get_by(array('username' => $_SESSION['_admin']['username'],'password' => md5($pass),'status' => 'active')))
    	{
    		$this->form_validation->set_message('_check_login','The %s is incorrect!');
    		return FALSE;
    	}
    	return TRUE;
    }
    
    function add()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            
    		$this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean')
    		                      ->set_rules('password', 'Password', 'required|trim|xss_clean')
    		                      ->set_rules('email', 'Email', 'required|valid_email|trim|xss_clean');    		                      ;

    		if($this->form_validation->run() == TRUE)
            {
                $data = array(
                    'username'    => $this->input->post('username'),
                    'password'    => md5($this->input->post('password')),
                    'email'       => $this->input->post('email'),
                    'group_id'    => 1,
                    'status'      => 'active',
                    'created_on'  => date('Y-m-d H:i:s'),
                );
                
                if($this->user_model->check_exist($data['username'])) {// Tồn tại username
                    die('This username not available !');
                }
                else {
                    $this->user_model->insert($data);
                    die('yes');
                }
            }
            else {
                die(validation_errors());
            }
        }
        
        //$data = array();
        $this->data['group_list'] = $this->user_model->get_group_list();
        $this->load->view('user/add', $this->data);
    }
    
    function edit($user_id = '')
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            
    		$this->form_validation->set_rules('password', 'Password', 'trim|xss_clean')
    		                      ->set_rules('email', 'Email', 'required|valid_email|trim|xss_clean');    		                      

    		if($this->form_validation->run() == TRUE) {
                $data = array(
                    'email'       => $this->input->post('email'),
                    'group_id'    => 1,
                    //'status'      => 'active',
                );
                
                if($pass = $this->input->post('password')) {
                    $data['password'] = md5($pass);
                }
                
                $this->user_model->update($user_id, $data);
				die('yes');
            }
            else {
                die(validation_errors());
            }
        }

        $data = array();
        $data['user']       = $this->user_model->get($user_id);
        $data['group_list'] = $this->user_model->get_group_list();
        
        $this->load->view('user/edit', $data);
    }

	/**
	 * @date	11.03.2011
	 */
	function delete()
	{
        if($_SERVER['REQUEST_METHOD'] == 'POST') {
    		$id = $this->input->post('id');
	        if(is_numeric($id)) {
	            $this->user_model->delete($id);
	        }
	        else { // multi : user_id is array
	            $this->user_model->delete_many($id);
	        }
            die('yes');
        }
	}

	/**
	 * @date	11.03.2011
	 */
    function load_row($id = null)
    {
        $this->data['user'] = $this->user_model->get($id);
        $this->load->view('user/row', $this->data);
    }
    
	/**
	 * @date	11.03.2011
	 */
	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {
			
			$id_list = $this->input->post('id');
			$action  = $this->input->post('action');
            
			if($action == 'delete') {
				$this->delete();
			}
			elseif($action == 'suspend') {
                $this->user_model->update_many($id_list, array('status' => 'suspend'));
			}
			elseif($action == 'active') {
				$this->user_model->update_many($id_list, array('status' => 'active'));
			}
			
			die('yes');
		}
	}
    
	/**
	 * @date	11.03.2011
	 */
    function change_status()
    {
		$id     = $this->input->post('user_id');
		$status = $this->input->post('status');
        
		$this->user_model->update_many($id, array('status' => $status));
		die('yes');
    }
}
